package com.karst.essentials.chapter04
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	internal class BitmapMaterialImageExample extends BasicView
	{
		public function BitmapMaterialImageExample()
		{
			super() ;
			createImgLoader() ;
			startRendering() ;
		}
		
		private var imgLoader : Loader = null ;
		private function createImgLoader() : void
		{
			if( imgLoader == null )
			{
				imgLoader = new Loader() ;
				imgLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, loadedHandler ) ;
				imgLoader.load( new URLRequest( "01.jpg" )) ;
			}
		}
		
		private var material : BitmapMaterial = null ;
		private var plane : Plane = null ;
		private function createView( $bmpData : BitmapData = null ) : void
		{
			if( material == null )
			{
				material = new BitmapMaterial( $bmpData, true ) ;
				material.doubleSided = true ;
				material.smooth = true ;
			}
			if( plane == null && material != null )
				plane = new Plane( material, 400, 400, 12, 10 ) ;
			
			scene.addChild( plane ) ;
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick( event ) ;
			if( plane )
				plane.localRotationY++ ;
		}
		
		private function loadedHandler( event : Event ) : void
		{
			createView( (imgLoader.content as Bitmap).bitmapData ) ;
		}
	}
}